<template>
  <div>
    <top-intro :data="{name:'香奈儿山茶花润泽水感乳液',imgUrl:require('@/images//Banner_2x.jpg')}"></top-intro>
    <div class="out-aj">
      <way-intro :way="way" v-if="!isDs"></way-intro>
      <div v-else class="text">留下你的信息，凭二维码至指定专柜领取体验装。一个手机号码限本人申领一次。</div>
      <keep-alive>
        <counter-form-dd v-if="isDs"></counter-form-dd>
        <counter-form v-else></counter-form>
      </keep-alive>
    </div>
  </div>
</template>

<script>
import TopIntro from "@/components/TopIntro";
import WaySelect from "@/components/WaySelect";
import WayIntro from "@/components/WayIntro";
import CounterForm from "@/components/CounterForm";
import CounterFormDd from "@/d21/CounterForm";
import * as Api from "@/service";
import utils from "@/utils";
export default {
  data() {
    return {
      way: "COUNTER",
      isDs: false
    };
  },
  created() {
    this.over();
    this.getInfo();
    this.getMax();
  },
  components: {
    TopIntro,
    WaySelect,
    WayIntro,
    CounterForm,
    CounterFormDd
  },
  methods: {
    over() {
      // let discern = localStorage.getItem("camellia-sample-discern");
      // console.log(discern)
      // console.log(utils.isOver(discern))
      // if (utils.isOver(discern)) {
      //   this.$router.push("/over");
      // }
    },
    getWay(val) {
      this.way = val;
    },
    getInfo() {
      console.log(utils.getChannelInfo());
      if (utils.getChannelInfo().type === "ds") {
        this.isDs = true;
      }
    },
    setTypeLimit(type, limit) {
      localStorage.setItem("camellia-sample-type", type);
      localStorage.setItem("camellia-sample-limit", limit);
    },
    async getMax() {
      try {
        let type = ""; //渠道类型
        const channel_info = utils.getChannelInfo(); //渠道信息 channel value type
        let channel = channel_info.channel;
        let limitInfo = await Api.getLimit(); //获取所有渠道信息
        let dataLimit = limitInfo.data;
        localStorage.setItem("camellia-sample-type", 0);
        dataLimit.forEach(element => {
          if (element.type === channel_info.type) {
            type = element.type;
            this.setTypeLimit(element.type, element.limit);
          }
        });
        const { data } = await Api.getMax({ type });
        if (data.limit === 0) {
          this.$router.push("/over");
          // localStorage.removeItem("camellia-sample-discern");
        }
      } catch (error) {
        // alert(error.message);
        console.log(error.message);
      }
    }
  }
};
</script>

<style lang="less" scoped>
.out-aj {
  padding: 0 24px 36px;
}
.text {
  margin-bottom: 20px;
  color: #666;
  font-size: 12px;
  line-height: 20px;
}
</style>
